package sjtu.ist.warehouse4ws.service.cust;

import java.util.ArrayList;
import java.util.List;

import sjtu.ist.warehouse4ws.dao.cust.CustDepartmentDao;
import sjtu.ist.warehouse4ws.dao.cust.CustFileInfoDao;
import sjtu.ist.warehouse4ws.model.CustFileinfo;
import sjtu.ist.warehouse4ws.vo.FullSearchVO;


public class CustFileInfoServiceImpl implements CustFileInfoService {
	private CustDepartmentDao custDepartmentDao;
	private CustFileInfoDao custFileInfoDao;

	public void setCustFileInfoDao(CustFileInfoDao custFileInfoDao) {
		this.custFileInfoDao = custFileInfoDao;
	}
	
	public void setCustDepartmentDao(CustDepartmentDao custDepartmentDao) {
		this.custDepartmentDao = custDepartmentDao;
	}

	@Override
	public List<CustFileinfo> getCustFileInfoList() {
		// TODO Auto-generated method stub
		return (List<CustFileinfo>)custFileInfoDao.getAllCustFileInfos();
	}

	@Override
	public void saveCustFileInfo(CustFileinfo custFileInfo) {
		// TODO Auto-generated method stub
		this.custFileInfoDao.save(custFileInfo);
	}

	@Override
	public List<CustFileinfo> findCustFileInfoByName(String fileName) {
		// TODO Auto-generated method stub
		return custFileInfoDao.findByName(fileName);
	}
	@Override
	public List<CustFileinfo> searchFileInfo(String keyWord) {
		//模型普通搜索：根据关键字返回对应的文件信息
		return custFileInfoDao.findByKeyword(keyWord);
	}
	
	@Override
	public List<CustFileinfo> searchDetailFileInfo(String keyWord1, String keyWord2, String keyWord3, 
												String keyWord4, String keyWord5, String keyWord6) {
		//模型高级搜索
		return custFileInfoDao.findDetailByKeyword(keyWord1,keyWord2,keyWord3,
												keyWord4,keyWord5,keyWord6);
	}
	@Override
	public void updateCustFileInfo(CustFileinfo custFileInfo) {
		// TODO Auto-generated method stub
		custFileInfoDao.update(custFileInfo);
	}
	@Override
	public CustFileinfo getCustFileInfo(String id) {
		// TODO Auto-generated method stub
		return custFileInfoDao.get(id);
	}
	
	//普通搜索：转换整个FileList的department信息
	@Override
	public List<FullSearchVO> getAllFullSearchResult(String keyWord) {
		List<CustFileinfo> flist=custFileInfoDao.findByKeyword(keyWord);
		List<FullSearchVO> fsvolist=new ArrayList<FullSearchVO>();
		for(CustFileinfo f : flist){
			FullSearchVO fsvo=getFullSearchResult(f);
			fsvolist.add(fsvo);
		}
		return fsvolist;
	}
	
	//高级搜索：转换整个FileList的department信息
	@Override
	public List<FullSearchVO> getAllDetailSearchResult(String keyWord1, String keyWord2, String keyWord3,
			String keyWord4, String keyWord5, String keyWord6) {
		List<CustFileinfo> flist=custFileInfoDao.findDetailByKeyword(keyWord1,keyWord2,keyWord3,
				keyWord4,keyWord5,keyWord6);
		List<FullSearchVO> fsvolist=new ArrayList<FullSearchVO>();
		for(CustFileinfo f : flist){
			FullSearchVO fsvo=getFullSearchResult(f);
			fsvolist.add(fsvo);
		}
		return fsvolist;
	}
	
	//获得vo
	private FullSearchVO getFullSearchResult(CustFileinfo file){
		CustFileinfo fileInfo=custFileInfoDao.get(file.getId());
		FullSearchVO fsvo=new FullSearchVO();
		fsvo.setId(fileInfo.getId());
		fsvo.setDepartment(custDepartmentDao.findById(fileInfo.getDepartment()).getCnName());
		fsvo.setName(fileInfo.getName());
		fsvo.setProject(fileInfo.getProject());
		fsvo.setType(fileInfo.getType());
		fsvo.setVersion(fileInfo.getVersion());
		return fsvo;
	}

	@Override
	public void deleteCustFileInfo(String id) {
		// TODO Auto-generated method stub
		custFileInfoDao.delete(id);
	}
	
	@Override
	public List<CustFileinfo> findFileId(String fileName, String project, int type) {
		// TODO Auto-generated method stub
		return custFileInfoDao.getFileId(fileName,project,type);
	}
}
